<template>
  <div class="app-container system-user">
    <!-- 搜索表单区域 -->
    <el-form
      :inline="true"
      :model="searchForm"
      class="form-search"
      size="small"
    >
      <el-form-item label="用户账号">
        <el-input
          v-model="searchForm.userName"
          placeholder="用户账号"
        ></el-input>
      </el-form-item>
      <el-form-item label="手机号码">
        <el-input
          v-model="searchForm.userPhone"
          placeholder="手机号码"
        ></el-input>
      </el-form-item>
      <el-form-item label="状态">
        <el-select v-model="searchForm.roles" placeholder="用户状态">
          <el-option label="正常" value="shanghai"></el-option>
          <el-option label="停用" value="beijing"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          size="mini"
          icon="el-icon-search"
          @click="getAllUser"
        >
          查询
        </el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetSearch">
          重置
        </el-button>
      </el-form-item>
    </el-form>
    <!-- 按钮区域 -->
    <div class="btn-box">
      <el-button
        size="mini"
        type="primary"
        icon="el-icon-plus"
        @click="dialogAddVisible = true"
      >
        新增
      </el-button>
      <div>
        <el-tooltip class="item" effect="dark" content="刷新" placement="top">
          <el-button
            size="mini"
            circle
            icon="el-icon-refresh"
            :disabled="loading"
            @click="getAllUser"
          ></el-button>
        </el-tooltip>
      </div>
    </div>
    <!-- 表格区域 -->
    <el-table
      v-loading="loading"
      :data="tableData"
      style="width: 100%"
      :header-cell-style="{ background: '#F8F8F9', color: '#606266' }"
    >
      <!-- 表格折叠区域 -->
      <el-table-column type="expand">
        <template slot-scope="scope">
          <el-form label-position="left" inline class="demo-table-expand">
            <el-form-item label="用户ID">
              <span>{{ scope.row.userId }}</span>
            </el-form-item>
            <el-form-item label="用户账号">
              <span>{{ scope.row.userName }}</span>
            </el-form-item>
            <el-form-item label="姓名">
              <span>{{ scope.row.realName }}</span>
            </el-form-item>
            <el-form-item label="性別">
              <span>{{ scope.row.userSex }}</span>
            </el-form-item>
            <el-form-item label="手机号码">
              <span>{{ scope.row.userPhone }}</span>
            </el-form-item>
            <el-form-item label="邮箱">
              <span>{{ scope.row.userEmail }}</span>
            </el-form-item>
            <el-form-item label="角色">
              <span>{{ scope.row.userRole.roleName }}</span>
            </el-form-item>
            <el-form-item label="用户状态">
              <span>{{ scope.row.userStatus == 0 ? '正常' : '停用' }}</span>
            </el-form-item>
            <el-form-item label="归属部门">
              <span v-if="scope.row.dept">{{ scope.row.dept.deptName }} </span>
              <span v-else>未分配</span>
            </el-form-item>
            <el-form-item label="职务">
              <span>{{ scope.row.userWork }}</span>
            </el-form-item>
            <el-form-item label="创建时间">
              <span>{{ scope.row.createTime }}</span>
            </el-form-item>
            <el-form-item label="更新时间">
              <span>{{ scope.row.updateTime }}</span>
            </el-form-item>
            <el-form-item label="备注">
              <span>{{ scope.row.userDesc }}</span>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column label="#" type="index"> </el-table-column>
      <el-table-column prop="userName" label="用户账号"> </el-table-column>
      <el-table-column prop="realName" label="姓名"> </el-table-column>
      <el-table-column prop="userPhone" label="电话"> </el-table-column>
      <el-table-column label="所属部门">
        <template slot-scope="scope">
          <span v-if="scope.row.dept">{{ scope.row.dept.deptName }}</span>
          <span v-else>未分配</span>
        </template>
      </el-table-column>
      <el-table-column prop="roleName" label="角色">
        <template slot-scope="scope">
          {{ scope.row.userRole.roleName }}
        </template>
      </el-table-column>
      <el-table-column prop="userStatus" label="用户状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.userStatus == 0">正常</el-tag>
          <el-tag v-else type="danger">停用</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间"></el-table-column>
      <el-table-column fixed="right" label="操作" header-align="center">
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            icon="el-icon-edit"
            @click="showEditDialog(scope.row)"
          >
            修改
          </el-button>
          <el-button
            type="text"
            size="small"
            icon="el-icon-delete"
            @click="deleteUser(scope.row)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页区域 -->
    <div class="pagination-warp">
      <el-pagination
        :current-page="current"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="limit"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      >
      </el-pagination>
    </div>
    <!-- 编辑、删除对话框 -->
    <AddDialog :dialog-visible="dialogAddVisible"></AddDialog>
    <EditDialog :dialog-visible="dialogEditVisible"></EditDialog>
  </div>
</template>

<script>
import { getAllUser, deleteUser } from '@/api/user';
import { getRoles } from '@/api/role';
import { getAllDept } from '@/api/dept';
import AddDialog from './components/addUserDialog';
import EditDialog from './components/editUserDialog';
export default {
  name: 'UserAdmin',
  components: {
    AddDialog,
    EditDialog
  },
  data() {
    return {
      loading: true, // 加载遮罩
      dialogEditVisible: false, // 编辑对话框显示与隐藏
      dialogAddVisible: false, // 新增对话框显示与隐藏
      tableData: [], // 表格数据
      current: 1, // 当前页
      limit: 10, // 一页几个
      total: 0, // 总数
      searchForm: {
        // 搜索表单
        roleId: '',
        userName: '',
        userPhone: ''
      },
      roles: [], // 所有角色
      deptOptions: [],
      currentUser: {
        userRole: {
          roleId: ''
        }
      } // 当前所在行的用户===编辑时用
    };
  },
  created() {
    this.getRoles();
    this.getAllUser();
    this.getAllDept();
  },
  methods: {
    /**
     * 处理分页修改
     */
    handleSizeChange(val) {
      this.limit = val;
      this.getAllUser();
    },
    handleCurrentChange(val) {
      this.current = val;
      this.getAllUser();
    },
    /**
     * 重置查询
     */
    resetSearch() {
      this.searchForm = {
        roleId: '',
        userName: '',
        userPhone: ''
      };
      this.getAllUser();
    },
    /**
     * 显示编辑对话框
     */
    showEditDialog(row) {
      this.currentUser = row;
      if (this.currentUser.dept === null) {
        this.currentUser.deptId = null;
      }
      this.dialogEditVisible = true;
    },
    /**
     * 获取所有角色
     */
    async getRoles() {
      const { data: res } = await getRoles(1, 9999);
      console.log('api=获取角色列表', res);
      const roles = res.data.roles;
      roles.forEach(item => {
        const roleId = item.roleId;
        const roleName = item.roleName;
        this.roles.push({ roleId, roleName });
      });
    },
    /**
     * 获取所有部门
     */
    async getAllDept() {
      const { data: res } = await getAllDept();
      console.log('api=获取所有部门', res);
      this.deptOptions = res.data.deptList;
    },
    /**
     * 获取所有用户
     */
    async getAllUser() {
      this.loading = true;
      const { data: res } = await getAllUser(
        this.current,
        this.limit,
        this.searchForm
      );
      this.total = res.data.total;
      console.log('api=获取用户列表', res);
      this.tableData = res.data.userList;
      this.loading = false;
    },
    /**
     * 删除用户
     */
    deleteUser(row) {
      console.log('删除用户', row.userId);
      this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const { data: res } = await deleteUser(row.userId);
          console.log('api=删除用户操作', res);
          if (res.code === 200) {
            // if (this.userStatus)
            this.$message.success('删除用户成功!');
            this.getAllUser();
          } else {
            this.$message.error(res.message);
          }
        })
        .catch(() => {});
    }
  }
};
</script>

<style lang="scss">
.form-search {
  .el-input {
    width: 240px;
  }
}
</style>

<style lang="scss" scoped>
.system-user {
  .pagination-warp {
    margin-top: 20px;
    position: relative;
    .el-pagination {
      position: absolute;
      right: 0;
      top: 0;
    }
  }

  .el-table {
    margin-top: 10px;
  }

  .btn-box {
    display: flex;
    justify-content: space-between;
  }

  .demo-table-expand {
    font-size: 0;
  }
  .demo-table-expand label {
    width: 90px;
    color: #99a9bf;
  }
  .demo-table-expand .el-form-item {
    margin-right: 0;
    margin-bottom: 0;
    width: 50%;
  }
}
</style>
